package com.fzu.mmdbproxyframework.cluster;

import java.util.HashMap;
import java.util.UUID;

import com.fzu.mmdbproxyframework.cluster.passcloud.SendRequest;
import com.fzu.mmdbproxyframework.cluster.passcloud.VMOperation;
import com.fzu.mmdbproxyframework.context.ClusterContext;
import com.fzu.mmdbproxyframework.mmdb.h2.H2DB;
import com.fzu.mmdbproxyframework.model.DBInterf;
import com.fzu.mmdbproxyframework.model.DataNode;
import com.fzu.mmdbproxyframework.model.VM;
import com.fzu.mmdbproxyframework.util.UniqueStringGenerator;

public class VmManager {

	public static void main(String[] args) {
		deleteVM("9c890804-2a3e-4e6a-905b-2115816c8ed3");
			
	}
	
	public static VM creatVM(){
		VMOperation vmOperation = VMOperation.getInstance();		
			VM vm = new VM();
			vm.setMemory(1024);
			for(HashMap<String, Object> mapFlavors:vmOperation.listFlavors()){
				if(mapFlavors.get("memory")!=null && mapFlavors.get("memory").equals(vm.getMemory())){
//					System.out.println("mapFlavors=id"+mapFlavors.get("id").toString());
					vm.setServiceofferingid(mapFlavors.get("id").toString());
					break;
				}
			}
			for(HashMap<String, Object> mapTemplates:vmOperation.listTemplates("")){
				if(mapTemplates.get("name").equals("h2Server")){
//					System.out.println("listTemplates=id"+mapTemplates.get("id").toString());
					vm.setTemplateid(mapTemplates.get("id").toString());
					break;
				}
			}
			for(HashMap<String, Object> mapStorages:vmOperation.listStorages()){
				if(mapStorages.get("disksize")!=null && mapStorages.get("disksize").equals(10)){
//					System.out.println("mapStorages=id"+mapStorages.get("id").toString());
					vm.setDiskofferingid(mapStorages.get("id").toString());
					break;
				}
				
			}
			for(HashMap<String, Object> mapZones:vmOperation.listZones()){
				if(mapZones.get("name").equals("DataCenter")){
//					System.out.println("mapZones=id"+mapZones.get("id").toString());
					vm.setZoneid(mapZones.get("id").toString());
					break;
				}
				
			}
			for(HashMap<String, Object> mapVMNetwork:vmOperation.listVMNetwork()){
				if(mapVMNetwork.get("name").equals("SharedNetwork")){
//					System.out.println("mapVMNetwork=id"+mapVMNetwork.get("id"));
					vm.setNetworkid(mapVMNetwork.get("id").toString());
					break;
				}
				
			}
			String name =UniqueStringGenerator.generateRandomString("h2Server");
			vm.setDisplayname(name);
			vm.setName(name);
			HashMap<String, String> params = new HashMap<String, String>();
			params.put("serviceofferingid", vm.getServiceofferingid());
//			params.put("diskofferingid", vm.getDiskofferingid());
			params.put("templateid", vm.getTemplateid());
			params.put("zoneid", vm.getZoneid());
			params.put("networkids", vm.getNetworkid());
			params.put("name", vm.getName());
			params.put("displayname", vm.getDisplayname());
			HashMap<String, String> vmIP_ID =vmOperation.deployVM(params);
			if(vmIP_ID == null){
				vm.setCreateSuccess(false);
				vm.setIp(null);
				vm.setUuid(null);
			}else{
				vm.setIp(vmIP_ID.get("ip"));
				vm.setUuid(vmIP_ID.get("id"));
				vm.setCreateSuccess(true);
			}
		
		try {
			Thread.sleep(6000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return vm;
	}
	public static void creatVM(int size){
		VMOperation vmOperation = VMOperation.getInstance();
		for(int i=1;i<=size;i++){
			VM vm = new VM();
			vm.setMemory(1024);
			for(HashMap<String, Object> mapFlavors:vmOperation.listFlavors()){
				if(mapFlavors.get("memory")!=null && mapFlavors.get("memory").equals(vm.getMemory())){
	//					System.out.println("mapFlavors=id"+mapFlavors.get("id").toString());
					vm.setServiceofferingid(mapFlavors.get("id").toString());
					break;
				}
			}
			for(HashMap<String, Object> mapTemplates:vmOperation.listTemplates("")){
				if(mapTemplates.get("name").equals("h2Server")){
	//					System.out.println("listTemplates=id"+mapTemplates.get("id").toString());
					vm.setTemplateid(mapTemplates.get("id").toString());
					break;
				}
			}
			for(HashMap<String, Object> mapStorages:vmOperation.listStorages()){
				if(mapStorages.get("disksize")!=null && mapStorages.get("disksize").equals(10)){
	//					System.out.println("mapStorages=id"+mapStorages.get("id").toString());
					vm.setDiskofferingid(mapStorages.get("id").toString());
					break;
				}
				
			}
			for(HashMap<String, Object> mapZones:vmOperation.listZones()){
				if(mapZones.get("name").equals("DataCenter")){
	//					System.out.println("mapZones=id"+mapZones.get("id").toString());
					vm.setZoneid(mapZones.get("id").toString());
					break;
				}
				
			}
			for(HashMap<String, Object> mapVMNetwork:vmOperation.listVMNetwork()){
				if(mapVMNetwork.get("name").equals("SharedNetwork")){
	//					System.out.println("mapVMNetwork=id"+mapVMNetwork.get("id"));
					vm.setNetworkid(mapVMNetwork.get("id").toString());
					break;
				}
				
			}
			String name =UniqueStringGenerator.generateRandomString("h2Server");
			vm.setDisplayname(name);
			vm.setName(name);
			HashMap<String, String> params = new HashMap<String, String>();
			params.put("serviceofferingid", vm.getServiceofferingid());
	//			params.put("diskofferingid", vm.getDiskofferingid());
			params.put("templateid", vm.getTemplateid());
			params.put("zoneid", vm.getZoneid());
			params.put("networkids", vm.getNetworkid());
			params.put("name", vm.getName());
			params.put("displayname", vm.getDisplayname());
			HashMap<String, String> vmIP_ID =vmOperation.deployVM(params);
			if(vmIP_ID == null){
				vm.setCreateSuccess(false);
				vm.setIp(null);
				vm.setUuid(null);
			}else{
				vm.setIp(vmIP_ID.get("ip"));
				vm.setUuid(vmIP_ID.get("id"));
				vm.setCreateSuccess(true);
			}
		}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static Boolean deleteVM(String uuid){
		System.out.println("开始删除虚拟机");
		VMOperation vmOperation = VMOperation.getInstance();
		Boolean success = vmOperation.destroyVM(uuid);
		System.out.println("deleteVM success="+success);
		return success;
	}
	
}
